global root_dir = "`1'"

include "$root_dir/code/config/config.do"

cap noi log using ${log_dir}/Table_A26_alm_robustness.log, replace name(tabl)

capture noi {  
    
    * Table_alm_robustness

    * We use three different industry-patent concordances:
    * DDCF = Mapped to using industry
    * LZ = Lybbert and Zolas mapping
    * DD = Mapped to manufacturing industry


    * Config
    global cvers "_ipc4"
    local min_pats 0 
    if (`min_pats' != 0) local restr "_min`min_pats'pats"

    capture program drop addMean
    program define addMean
        qui sum `1' [aw=avwt]
        local x = round(r(mean),0.01)
        estadd local weighted_mean `x'
    end

    * ---------------------------------
    * Run regression
    * ---------------------------------

    use ${final_dir}/ind6090_patents_regression_data${cvers}.dta, clear


    * 1: Biadic patents
    preserve
    keep if in_relevant_field_bia_use >= `min_pats'

    *1)  delta routine cognitive biadic
    reg dqsts sh_auto95_bia_use manuf computeruse [aw=avwt], vce(cluster uas_ind6090)
    addMean dqsts
    estadd local obs "{\num{`e(N)'}}"
    estimates store cogm_col1

    *4) delta routine manual biadic
    reg dqfing sh_auto95_bia_use manuf computeruse [aw=avwt], vce(cluster uas_ind6090)
    addMean dqfing
    estadd local obs "{\num{`e(N)'}}"
    estimates store manm_col1

    *7) delta high/low skill workers biadic
    reg dratioHL sh_auto95_bia_use manuf computeruse [aw=avwt], vce(cluster uas_ind6090)
    addMean dratioHL
    estadd local obs "{\num{`e(N)'}}"
    estimates store hlrm_col1

    *10) delta labor share NBER biadic
    reg d_lshare sh_auto95_bia_use computeruse [aw=avwt], vce(cluster uas_ind6090) 
    addMean dqsts
    estadd local obs "{\num{`e(N)'}}"
    estimates store lsm_col1

    restore

    * 2: Auto90 patents
    preserve
    keep if in_relevant_field_use  >= `min_pats'

    *2) delta routine cognitive auto90
    reg dqsts sh_auto90_use computeruse manuf [aw=avwt], vce(cluster uas_ind6090)
    addMean dqsts
    estadd local obs "{\num{`e(N)'}}"
    estimates store cogm_col2

    *5) delta routine manual auto90
    reg dqfing sh_auto90_use manuf computeruse [aw=avwt], vce(cluster uas_ind6090)
    addMean dqfing
    estadd local obs "{\num{`e(N)'}}"
    estimates store manm_col2

    *8) delta high/low skill workers auto90
    reg dratioHL sh_auto90_use manuf computeruse [aw=avwt], vce(cluster uas_ind6090)
    addMean dratioHL
    estadd local obs "{\num{`e(N)'}}"
    estimates store hlrm_col2

    *11) delta labor share NBER auto90
    reg d_lshare sh_auto90_use computeruse [aw=avwt], vce(cluster uas_ind6090)
    addMean dqfing
    estadd local obs "{\num{`e(N)'}}"
    estimates store lsm_col2

    restore

    * 3: Auto95 patents with LZ

    
    preserve
    keep if in_relevant_field_lz  >= `min_pats'
    keep if in_lz == 1

    * with manuf
    *3) delta routine cognitive Lybbert & Zolas
    reg dqsts sh_auto95_lz computeruse manuf [aw=avwt], vce(cluster uas_ind6090)
    addMean dqsts
    estadd local obs "{\num{`e(N)'}}"
    estimates store cogm_col3

    *6) delta routine manual Lybbert & Zolas
    reg dqfing sh_auto95_lz computeruse manuf [aw=avwt], vce(cluster uas_ind6090)
    addMean dqfing
    estadd local obs "{\num{`e(N)'}}"
    estimates store manm_col3

    *9) delta high/low skill workers Lybbert & Zolas
    reg dratioHL sh_auto95_lz computeruse manuf [aw=avwt], vce(cluster uas_ind6090)
    addMean dratioHL
    estadd local obs "{\num{`e(N)'}}"
    estimates store hlrm_col3

    *12) delta labor share NBER Lybbert & Zolas
    reg d_lshare sh_auto95_lz computeruse [aw=avwt], vce(cluster uas_ind6090)
    addMean dratioHL
    estadd local obs "{\num{`e(N)'}}"
    estimates store lsm_col3

    restore

    * Output table
    qui do ${code_dir}/config/tabletools.do
    setlabels

    dhoztab cogm_col1 cogm_col2 cogm_col3 manm_col1 manm_col2 manm_col3 hlrm_col1 hlrm_col2 hlrm_col3 lsm_col1 lsm_col2 lsm_col3 using ${tab_dir}/appendix/Table_A26_alm_robustness.tex,  b(%10.2f) se(%10.2f) nogaps replace nolines numbers nonotes nolines showdepvarinfo nomtitles sfmt(%10.2f a1) scalars("r2 \noalign{\vskip 1.8mm} R$^2$" "obs Industries") label drop(_cons) noobs rename(sh_auto95_use sh_auto sh_auto95_bia_use sh_auto sh_auto90_use sh_auto sh_auto95_lz sh_auto) ///
        depvar("none") ////
        rawaddheader(&\multicolumn{3}{c}{$\Delta$ Routine cognitive} & \multicolumn{3}{c}{$\Delta$ Routine manual} &  \multicolumn{3}{c}{$\Delta$ High/low skill workers} &  \multicolumn{3}{c}{$\Delta$ Labor Share (NBER)}  \\ \cmidrule(lr){2-4}  \cmidrule(lr){5-7} \cmidrule(lr){8-10} \cmidrule(lr){11-13}) ///
        mgroups("Biadic" "Auto90" "\shortstack{Lybbert \\ and Zolas}" "Biadic" "Auto90" "\shortstack{Lybbert \\ and Zolas}" "Biadic" "Auto90" "\shortstack{Lybbert \\ and Zolas}" "Biadic" "Auto90" "\shortstack{Lybbert \\ and Zolas}", pattern(1 1 1 1 1 1 1 1 1 1 1 1) span prefix(\multicolumn{@span}{c}{) suffix(})) ///
        order(sh_auto computeruse manuf) ///
        notes("This table provides robustness checks for the effect of automation technologies on tasks, skill composition, and the labor-share. Columns 1, 4, 7, and 10 use biadic auto95 patents: that is, patents applied for in at least two countries. Columns 2, 5, 8, and 11 define automation patents as auto90 patents. In both cases, patents are allocated to their sector of use. Columns 3, 6, 9, and 12 use auto95 patents (as in the baseline) but allocate patents using a concordance table between C/IPC codes and industries from Lybbert and Zolas (2014). The regressions are weighed by the mean industry share of total employment in FTEs in 1980 and 1998. Standard errors are clustered at the level of industry groups that have the same automation share by construction and reported in parentheses.") 
    *again, brackets }
}
if _rc == 0 {
    display "Execution finished successfully."
}
else {
    display "Execution finished with errors."
}
